package com.md.common.utils.sms.chuanglan.sms;

/**
 * 功能描述：
 *
 * @author lujianping@oak168.com
 * @version 1.0
 * @date 2018-07-12 15:02
 * @since JDK 1.8
 */

import com.alibaba.fastjson.JSON;
import com.md.common.utils.sms.chuanglan.sms.request.SmsSendRequest;
import com.md.common.utils.sms.chuanglan.sms.response.SmsSendResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 * @author tianyh
 * @Description:HTTP 请求
 */
public class ChuangLanSmsUtil {
    private static Logger LOGGER = LoggerFactory.getLogger(ChuangLanSmsUtil.class);

    //用户平台API密码(非登录密码)
    public static String password = "52WfSxp0By6beb";
    public static String account = "N7230253";
    public static String path = "http://smssh1.253.com/msg/send/json";

    /**
     * @author ljp
     * @Description
     * @param smsSendRequest
     * @return
     */
    public static SmsSendResponse sendSmsByPost(SmsSendRequest smsSendRequest) {
        smsSendRequest.setAccount(account);
        smsSendRequest.setPassword(password);
		smsSendRequest.setReport("false");
        String requestJson = JSON.toJSONString(smsSendRequest);
        String response = sendSmsByPost(requestJson);
        SmsSendResponse result = null;
        if(StringUtils.isNoneEmpty(response)){
            result = JSON.parseObject(response, SmsSendResponse.class);
        }
        return result;
    }

    /**
     *
     * @author tianyh
     * @Description
     * @param postContent
     * @return String
     * @throws
     */
    public static String sendSmsByPost(String postContent) {
        String bizDesc = "调用创蓝短信接口";
        LOGGER.info("{} start", bizDesc);
        LOGGER.info("{}，请求地址：{}，请求参数：{}", bizDesc, path, postContent);
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
            httpURLConnection.setReadTimeout(10000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");

            httpURLConnection.connect();
            OutputStream os=httpURLConnection.getOutputStream();
            os.write(postContent.getBytes("UTF-8"));
            os.flush();

            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                // 开始获取数据
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                LOGGER.info("{}成功，响应参数：{}", bizDesc, sb.toString());
                LOGGER.info("{} end", bizDesc);
                return sb.toString();
            }else{
                LOGGER.info("{}失败", bizDesc);
            }
        } catch (Exception e) {
            LOGGER.info("{}发生异常", bizDesc, e);
            e.printStackTrace();
        }
        LOGGER.info("{} end", bizDesc);
        return null;
    }

}
